﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class AreaInsertViewModel : NotificationObject
    {
        AreaInsert gWindow;
        AreaViewModel gParentViewModel;

        public AreaInsertViewModel(AreaInsert pWindow, AreaViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            Initialize();
        }

        public void Initialize()
        {

        }

        private String areaName;
        public String AreaName
        {
            get
            {
                return areaName;
            }
            set
            {
                areaName = value;
                RaisePropertyChanged("AreaName");
            }
        }

        private int personnelUID;
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(CanInsertCommand));
            }
        }

        protected virtual void CanInsertCommand(object obj)
        {
            //添加用户组
            List<WArea> list = new List<WArea>();
            WArea area = new WArea();
            area.AreaName = AreaName;
            area.PersonnelUID = PersonnelUID;
            list.Add(area);

            HttpResponse res = WArea.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(CanQuitCommand));
            }
        }

        protected virtual void CanQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }
    }
}
